 var user_code = null;
var user_name = null;
var userPassword = null;
var ruserPassword = null;
var birthday = null;
var email = null;
var mobile = null;
var address = null;
var submit = null;
var reset = null;
var pic = null;
var errorinfo = null;
var errorinfo_wp = null;
var a_idPicPath = null;

$(function(){
	var path = $("#path").val();
	
	var imgYes = "<img width='15px' src='"+path+"/static/images/y.png' />";
	var imgNo = "<img width='15px' src='"+path+"/static/images/n.png' />";
	user_code = $("#user_code");
	user_name = $("#user_name");
	userPassword = $("#userPassword");
	ruserPassword = $("#ruserPassword");
	mobile = $("#mobile");
	birthday = $("#birthday");
	email = $("#email");
	submit = $("#add");
	reset = $("#back");

	errorinfo = $("#errorinfo");
	a_idPicPath = $("#a_idPicPath");
	errorinfo_wp = $("#errorinfo_wp");
	//初始化的时候，要把所有的提示信息变为：* 以提示必填项，更灵活，不要写在页面上
	user_code.next().html("*");
	user_name.next().html("*");
	
	
	userPassword.next().html("*");
	ruserPassword.next().html("*");
	mobile.next().html("*");
	birthday.next().html("*");
	email.next().html("*");
	if(errorinfo.val() == null || errorinfo.val() == ""){
		a_idPicPath.next().html("* 上传大小不能超过500K * 上传文件类型必须为：jpg、jpeg、png、pneg");
	}else{
		a_idPicPath.next().html(errorinfo.val());
	}
	

	
	
	
	
	/* * 验证
	 * 失焦\获焦
	 * jquery的方法传递*/
	
	user_code.bind("blur",function(){
		//ajax后台验证--user_code是否已存在
		
		var code=$("#user_code").val();
		
		
		$.ajax({
			type:"GET",//请求类型
			url:path+"/ucexist",//请求的url
			data:{user_code:code},//请求参数
			dataType:"json",//ajax接口（请求url）返回的数据类型
			success:function(data){//data：返回数据（json对象）
				if(data.user_code == "exist"){//账号已存在，错误提示
					validateTip(user_code.next(),{"color":"red"},imgNo+ " 该用户账号已存在",false);
				}else if(data.user_code=="existEmpty"){
					validateTip(user_code.next(),{"color":"red"},imgNo+ " 用户账号不能为空",false);
				}else{//账号可用，正确提示
					validateTip(user_code.next(),{"color":"green"},imgYes+" 该账号可以使用",true);
				}
			},
			error:function(data){//当访问时候，404，500 等非200的错误状态码
				validateTip(user_code.next(),{"color":"red"},imgNo+" 您访问的页面不存在",false);
			}
		});
	}).bind("focus",function(){
		//显示友情提示
		validateTip(user_code.next(),{"color":"#666666"},"* 用户编码是您登录系统的账号",false);
	}).focus();
	
	user_name.bind("focus",function(){
		validateTip(user_name.next(),{"color":"#666666"},"* 用户名长度必须是大于1小于10的字符",false);
	}).bind("blur",function(){
		if(user_name.val() != null && user_name.val().length > 1
				&& user_name.val().length < 10){
			validateTip(user_name.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(user_name.next(),{"color":"red"},imgNo+" 用户名输入的不符合规范，请重新输入",false);
		}
		
	});
	
	userPassword.bind("focus",function(){
		validateTip(userPassword.next(),{"color":"#666666"},"* 密码长度必须是大于6小于20",false);
	}).bind("blur",function(){
		if(userPassword.val() != null && userPassword.val().length > 6
				&& userPassword.val().length < 20 ){
			validateTip(userPassword.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(userPassword.next(),{"color":"red"},imgNo + " 密码输入不符合规范，请重新输入",false);
		}
	});
	
	ruserPassword.bind("focus",function(){
		validateTip(ruserPassword.next(),{"color":"#666666"},"* 请输入与上面一只的密码",false);
	}).bind("blur",function(){
		if(ruserPassword.val() != null && ruserPassword.val().length > 6
				&& ruserPassword.val().length < 20 && userPassword.val() == ruserPassword.val()){
			validateTip(ruserPassword.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(ruserPassword.next(),{"color":"red"},imgNo + " 两次密码输入不一致，请重新输入",false);
		}
	});
	
	
	birthday.bind("focus",function(){
		validateTip(birthday.next(),{"color":"#666666"},"* 点击输入框，选择日期",false);
	}).bind("blur",function(){
		if(birthday.val() != null && birthday.val() != ""){
			validateTip(birthday.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(birthday.next(),{"color":"red"},imgNo + " 选择的日期不正确,请重新输入",false);
		}
	});
	
	mobile.bind("focus",function(){
		validateTip(mobile.next(),{"color":"#666666"},"* 请输入手机号",false);
	}).bind("blur",function(){
		var patrn=/^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
		if(mobile.val().match(patrn)){
			validateTip(mobile.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(mobile.next(),{"color":"red"},imgNo + " 您输入的手机号格式不正确",false);
		}
	});
	
	email.bind("focus",function(){
		validateTip(email.next(),{"color":"#666666"},"* 请输入邮箱",false);
	}).bind("blur",function(){
		var patrn=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
		if(email.val().match(patrn)){
			validateTip(email.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(email.next(),{"color":"red"},imgNo + " 您输入的邮箱格式不正确",false);
		}
	});
	
	submit.bind("click",function(){
		if(user_code.attr("validateStatus") != "true"){
			user_code.blur();
		}else if(user_name.attr("validateStatus") != "true"){
			user_name.blur();
		}else if(userPassword.attr("validateStatus") != "true"){
			userPassword.blur();
		}else if(ruserPassword.attr("validateStatus") != "true"){
			ruserPassword.blur();
		}else if(birthday.attr("validateStatus") != "true"){
			birthday.blur();
		}else if(mobile.attr("validateStatus") != "true"){
			mobile.blur();
		}else{
			if(confirm("是否确认提交数据")){
				$("#userForm").submit();
			}
		}
	});
	
	
});

function validateTip(element,css,tipString,status){
	element.css(css);
	element.html(tipString);
	element.prev().attr("validateStatus",status);
}